//
//  LatestFunPicsViewController.m
//  SmileWeather
//
//  Created by lanou3g on 15/9/16.
//  Copyright (c) 2015年 陶天海. All rights reserved.
//

#import "LatestFunPicsViewController.h"
#import "UMSocial.h"
@interface LatestFunPicsViewController ()<UMSocialUIDelegate>
@property (nonatomic, strong) NSMutableArray *PicsArray;
@property (nonatomic,retain) MBProgressHUD * hud;
@end

@implementation LatestFunPicsViewController



- (void)reloadLatestPics {
    
    
    [LORequestManger GET:kLatestPics success:^(id response) {
        NSDictionary  *dic = (NSDictionary *)response;
        NSDictionary *Dic = dic[@"result"];
        for (NSDictionary *pic in Dic[@"data"]) {
            FunPicModel  *PicModel = [FunPicModel shareJsonWithDictionary:pic];
            
            
            NSLog(@"________%@", PicModel);
            
            
            [self.PicsArray addObject:PicModel];
            
            
        }
          [_hud hide:YES];
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        
        NSLog(@"%@",error);
        
    }];
    
    
    
}
- (NSMutableArray *)PicsArray {
    if (!_PicsArray) {
        _PicsArray = [[NSMutableArray alloc] init];
    }
    
    return _PicsArray;
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    BlurImageView *blurimageView = [[BlurImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
      self.tableView.backgroundView = blurimageView;
    [self p_setupProgressHud];
    [self reloadLatestPics];
    //self.view.backgroundColor = [UIColor blackColor];
    [self.tableView registerClass:[FunPicsCell class] forCellReuseIdentifier:@"funpicCell"];
    __block LatestFunPicsViewController *root = self;
    [self.tableView addHeaderWithCallback:^{
        NSLog(@"111");
       
        [root.tableView reloadData];
        [root.tableView headerEndRefreshing];
    }];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
- (void)p_setupProgressHud
{
    self.hud = [[MBProgressHUD alloc] initWithView:self.view];
    _hud.frame = self.view.bounds;
    _hud.minSize = CGSizeMake(50, 50);
    _hud.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_hud];
    
    [_hud show:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Potentially incomplete method implementation.
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete method implementation.
    // Return the number of rows in the section.
    return self.PicsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   FunPicsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"funpicCell" forIndexPath:indexPath];
  
    FunPicModel *funmodel = self.PicsArray[indexPath.row];
//    
//    NSURL *url = [NSURL URLWithString:model.url];
//    [cell.songsPic sd_setImageWithURL:url placeholderImage :[UIImage imageNamed:@"placeHoderImage.png"]];
    // Configure the cell...
   // cell.songListName.title.text = model.content;
    [cell  translateModel:funmodel tableView:tableView];
    [cell.songsIcon addTarget:self action:@selector(clickButton:) forControlEvents:(UIControlEventTouchUpInside)];
    
    cell.songsIcon.tag = 4000 + indexPath.row;
    
    cell.songsIcon.selected = NO;
    [cell.shareButton addTarget:self action:@selector(clickShare:) forControlEvents:(UIControlEventTouchUpInside)];
    cell.shareButton.tag = 2000 + indexPath.row;
    NSMutableArray *modelArray = [[PlayerDataBase shareDataBase] selegetAllFromPicTable];
    
    for (FunPicModel *model in modelArray) {
        if ([funmodel.content isEqualToString:model.content]) {
            cell.songsIcon.selected = YES;
        }
        
    }
    
    cell.songsPic.layer.anchorPoint = CGPointMake(0, 0);
    
    cell.songsPic.layer.position = cell.songsPic.layer.anchorPoint;
    cell.songsPic.transform = CGAffineTransformMakeRotation(0.22);//动画
    [UIView  animateWithDuration:0.3 animations:^{
        //self.animationImgeView.backgroundColor = [UIColor blueColor];
        
        
        
    }completion:^(BOOL finished) {
        [UIView animateWithDuration:1.5 animations:^{
            // self.animationImgeView.backgroundColor = [UIColor redColor];
            // imageView.transform = CGAffineTransformMakeRotation(M_PI);
            cell.songsPic.transform = CGAffineTransformRotate(cell.songsPic.transform, (-0.22));
            
            
        }];
        
        
        
    }];
    
    
    return cell;
}
- (void)clickShare:(UIButton *)button {
     FunPicModel *funmodel = self.PicsArray[button.tag - 2000];
    NSURL *url = [NSURL URLWithString:funmodel.url];
    NSData *data = [NSData dataWithContentsOfURL:url];
    //    [UMSocialConfig setSupportedInterfaceOrientations:UIInterfaceOrientationMaskLandscape];
    
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"5604fd14e0f55abb670003f8" shareText:nil shareImage:[UIImage imageWithData:data] shareToSnsNames:[NSArray arrayWithObjects:UMShareToDouban,UMShareToRenren,UMShareToSina,UMShareToTencent, nil] delegate:self];




}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kWidth/1.35;
}

- (void)clickButton:(UIButton *)button {

    button.selected = !button.selected;
    if (button.selected == YES) {
        //显示alertView提示用户
        UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
               
        //0.3秒后alertView消失
        [self performSelector:@selector(p_removeAlertView:) withObject:alertView afterDelay:0.3];
    }
    [[PlayerDataBase shareDataBase] createTables];
    [[PlayerDataBase shareDataBase]insertPicModelToPicTable:self.PicsArray[button.tag - 4000]];

}
- (void)p_removeAlertView:(UIAlertView *)alertView
{
    //alertView消失
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
